package com.my.bookstore.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.my.bookstore.exception.DBException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by YJH on 2017/7/17 22:15.
 */
public class JDBCUtils {

    private static DataSource dataSource = null;

    static {
        dataSource = new ComboPooledDataSource("javawebapp");
    }

    //获取数据库连接
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("数据库连接错误!");
        }
    }

    //关闭数据库连接
    public static void release(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("数据库连接错误!");
        }
    }

    //关闭数据库连接
    public static void release(ResultSet rs, Statement statement) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("数据库连接错误!");
        }

        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DBException("数据库连接错误!");
        }
    }

}
